package edu.uoc.tds.pac4.client.ss4;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import edu.uoc.tds.pac4.beans.Event;
import edu.uoc.tds.pac4.beans.Usuari;
import edu.uoc.tds.pac4.client.Client;
import edu.uoc.tds.pac4.client.PanelClientInterface;
import edu.uoc.tds.pac4.client.ss4.JPanelFilteHistoricEvents;
import edu.uoc.tds.pac4.common.GestioEventsException;
import edu.uoc.tds.pac4.common.PantallaError;
import edu.uoc.tds.pac4.common.TDSLanguageUtils;
import edu.uoc.tds.pac4.interfaces.RemoteInterface;

public class PantallaHistoricEvents extends PantallaInscripcions implements PanelClientInterface {

	private static final long serialVersionUID = 1L;
	private JPanel jPanelContainer = null;
	private JPanelFilteHistoricEvents jPanelFiltre = null;
	private jPanelLlistatHistoric jPanelEvents = null;
	private Vector<Event> currentEvents = null;
	private Client client= null;
	private JLabel jLabelUsuari = null;
	private JLabel jLabelFieldUsuari = null;
	private Usuari usuari = null;
	private JPanel jPanelTitol = null;
	private JLabel jLabelTitolPantalla = null;
	
	/**
	 * Constructor
	 */
	public PantallaHistoricEvents() {
		super();
		initialize();
	}

	/**
	 * Constructor que rep una referencia al servidor remot i client
	 * @param client  
	 * @param remot
	 */
	public PantallaHistoricEvents(Client client, RemoteInterface remot) {
		// TODO Auto-generated constructor stub
		this.remot = remot;
		this.client = client;
		initialize();
	}

	/**
	 * Inicialitza la pantalla
	 */
	protected void initialize() {
		usuari = client.getUserConnected();
	}

	/**
	 * This method initializes jPanelContainer
	 * 
	 * @return javax.swing.JPanel
	 */
	@Override
	public JPanel getJPanelContainer(Dimension dimension) {
		if (jPanelContainer == null) {
			Insets defaultInset = new Insets(10, 10, 10, 10);
			// Titol
			GridBagConstraints gbcPanelTitol = new GridBagConstraints();
			gbcPanelTitol.gridx = 0;
			gbcPanelTitol.gridy = 0;
			gbcPanelTitol.gridwidth = 3;
			gbcPanelTitol.gridheight = 3;
			gbcPanelTitol.fill = GridBagConstraints.HORIZONTAL;
			gbcPanelTitol.anchor = GridBagConstraints.PAGE_START;
			gbcPanelTitol.insets = defaultInset;
						
			
			//Filtre
			GridBagConstraints gbcPanelFiltre = new GridBagConstraints();
			gbcPanelFiltre.gridx = 0;
			gbcPanelFiltre.gridy = 4;
			
			//Llistat Events
			GridBagConstraints gbcPanelEvents = new GridBagConstraints();
			gbcPanelEvents.gridx = 0;
			gbcPanelEvents.gridy = 5;
			gbcPanelEvents.weightx = 1;
			gbcPanelEvents.weighty = 1;
			gbcPanelEvents.insets = new Insets(0, 10, 10, 10);
			gbcPanelEvents.fill = GridBagConstraints.BOTH;		
						
			jPanelContainer = new JPanel();
			jPanelContainer.setSize(dimension);
			jPanelContainer.setLayout(new GridBagLayout());
			
			jPanelContainer.add(getJPanelFiltre(), gbcPanelFiltre);
			jPanelContainer.add(getJPanelEvents(), gbcPanelEvents);	
			jPanelContainer.add(getJPanelTitol(), gbcPanelTitol);
			
		}
		return jPanelContainer;
	}
	/**
	 * This method initializes jPanelTitol
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelTitol() {
		if (jPanelTitol == null) {
			Insets titleInset = new Insets(10, 15, 10, 10);
			Insets textInset = new Insets(10, 5, 10, 25);
			
			// Usuari Participant
			GridBagConstraints gbcLabelUsuari = new GridBagConstraints();
			gbcLabelUsuari.gridx = 2;
			gbcLabelUsuari.gridy = 0;
			gbcLabelUsuari.insets = titleInset;
			jLabelUsuari = new JLabel();		
			jLabelUsuari.setText(TDSLanguageUtils.getMessage("Client.PantallaConsultaEvents.Usuari"));
			GridBagConstraints gbcTextUsuari = new GridBagConstraints();
			gbcTextUsuari.gridx = 3;
			gbcTextUsuari.gridy = 0;
			gbcTextUsuari.insets = textInset;
			jLabelFieldUsuari = new JLabel();
			jLabelFieldUsuari.setText(usuari.getNom()+" "+usuari.getCognoms()+ "     " );
	
			jLabelTitolPantalla = new JLabel(TDSLanguageUtils.getMessage("Client.PantallaHistoricEvents.Titol"));
			GridBagConstraints gbcTitolUsuari = new GridBagConstraints();
			gbcTitolUsuari.gridx = 1;
			gbcTitolUsuari.gridy = 0;
			gbcTitolUsuari.insets = titleInset;

			jPanelTitol = new JPanel();
			jPanelTitol.setLayout(new GridBagLayout());
			jPanelTitol.setBorder(BorderFactory.createLineBorder(Color.GRAY));
			jPanelTitol.add(jLabelTitolPantalla, gbcTitolUsuari);
			jPanelTitol.add(jLabelUsuari, gbcLabelUsuari);
			jPanelTitol.add(jLabelFieldUsuari, gbcTextUsuari);
		}
		return jPanelTitol;
	}

	
	
	/**
	 * This method initializes jPanelFiltre
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelFiltre() {
		if (jPanelFiltre == null) {
			jPanelFiltre = new JPanelFilteHistoricEvents(this);
		}
		return jPanelFiltre;
	}

	/**
	 * This method initializes jPanelEvents
	 * 
	 * @return JPanelEvents
	 */
	private jPanelLlistatHistoric getJPanelEvents() {
		if (jPanelEvents == null) {
			jPanelEvents = new jPanelLlistatHistoric();
		}
		return jPanelEvents;
	}
		
	/**
	 * Actualitza el contingut de la taula
	 */
	public void updateTaula() {		
		Date data = jPanelFiltre.getData();	
		String estat = jPanelFiltre.getEstat();
		if (estat.equals("OPEN")){
			// crida al servidor per rebre els events
			currentEvents = null;			
			try {				
				currentEvents = remot.getEvents(usuari.getCodi(), jPanelFiltre.getTipusEvent(), data, true);
			} catch (RemoteException e) {
				new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
			} catch (GestioEventsException e) {
				new PantallaError(e.getMessage(), PantallaError.Tipus.ERROR);
			}			
			jPanelEvents.drawEvents(currentEvents);
		} else { 
			if(estat.equals("CLOSED")){
				// crida al servidor per rebre els events
				currentEvents = null;			
				try {					
					currentEvents = remot.getEvents(usuari.getCodi(), jPanelFiltre.getTipusEvent(), data, false);
				} catch (RemoteException e) {
					new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
				} catch (GestioEventsException e) {
					new PantallaError(e.getMessage(), PantallaError.Tipus.ERROR);
				}		
				jPanelEvents.drawEvents(currentEvents);				
							
			}
		}
	}
}